package com.android.jblm.commandcentral.generators;

import android.util.Log;

import com.android.jblm.commandcentral.objects.Post;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Brian 2014.
 */
public class NewsBuilderNON_ASYNC {

    private static final String xmlUrl = "http://www.nwguardian.com/100/v-highlights/index.rss";

    private List<Post> thePosts;

    public NewsBuilderNON_ASYNC(){

        thePosts = new ArrayList<Post>();

        buildBook();

    }

    public int buildBook(){

        Post thePost = new Post();


        Log.w("NewsBuilder", "Post List Size BEFORE = " + thePosts.size() + "");

        int event;
        try{
            URL url = new URL(xmlUrl);
            HttpURLConnection conn = (HttpURLConnection)
                    url.openConnection();
            conn.setReadTimeout(10000 /* milliseconds */);
            conn.setConnectTimeout(15000 /* milliseconds */);
            conn.setRequestMethod("GET");
            conn.setDoInput(true);
            conn.connect();
            InputStream stream = conn.getInputStream();

            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            XmlPullParser xpp = factory.newPullParser();

            xpp.setInput(stream, null);

            event = xpp.getEventType();

            String type = "";
            String flag = "";

            Log.w("NewsBuilder", "BUILDING POSTS");

            while (event != XmlPullParser.END_DOCUMENT) {

                if(event == XmlPullParser.START_TAG){

                    System.out.println("Start of " + xpp.getName());
                    if(xpp.getName().equals("item") && flag.equals("post")){


                    } else if(xpp.getName().equals("managingEditor")){

                        flag = "post";
                    } else if(xpp.getName().equals("title") && flag.equals("post")){

                        type = "title";
                    } else if (xpp.getName().equals("link") && flag.equals("post")){
                        type = "link";

                    } else if (xpp.getName().equals("pubDate") && flag.equals("post")){
                        type = "pubDate";

                    } else if (xpp.getName().equals("description") && flag.equals("post")){
                        type = "description";

                    }
                } else if(event == XmlPullParser.TEXT && flag.equals("post")) {
                    if(!xpp.getText().replaceAll("[^A-Za-z0-9]", "").equals("")){

                        if(type.equals("item")){

                        } else if(type.equals("title")){

                            String post_title = xpp.getText().replaceAll("[\n,\t]", "");
                            thePost.setPostTitle(post_title);

                        } else if (type.equals("link")){
                            thePost.setPostURL(xpp.getText());

                        } else if (type.equals("pubDate")){
                            thePost.setPostDate(xpp.getText());

                            System.out.println("Date in: " + xpp.getText());
                        } else if (type.equals("description")){
                            thePost.setPostDesc(xpp.getText());

                        }
                    }

                } else if (event == XmlPullParser.END_TAG){
                    //close of post item, add to list
                    if (xpp.getName().equals("item")){

                        Log.w("NewsBuilder", "Post Title: " + thePost.getPostTitle() + "");
                        thePosts.add(thePost);
                        thePost = new Post();
                    }


                }

                event = xpp.next();


            }

        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }

        Log.w("NewsBuilder", "Post List Size AFTER = " + thePosts.size() + "");

        return 1;

    }

    public List<Post> getThePosts() {

        return thePosts;
    }
}
